package pers.qianyu.month_202012.date_20201207.sliding_window;

import org.junit.*;

/**
 * 3. 无重复字符的最长子串
 * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 *
 * @author mizzle rain
 * @date 2020-12-07 20:42
 */
public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int len = 1;
        int left = 0, right = 0;
        int[] dict = new int[128];
        while (right < s.length()) {
            char c = s.charAt(right);
            dict[c]++;
            while (dict[c] > 1) {
                dict[s.charAt(left++)]--;
            }
            if (right - left + 1 > len) {
                len = right - left + 1;
            }
            right++;
        }
        return len;
    }

    @Test
    public void test1() {
        String s = "abcabcbb";
        int result = new LengthOfLongestSubstring().lengthOfLongestSubstring(s);
        System.out.println("result = " + result);
    }

    @Test
    public void test2() {
        String s = "pwwkew";
        int result = new LengthOfLongestSubstring().lengthOfLongestSubstring(s);
        System.out.println("result = " + result);
    }
}
